package com.yuexiao.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yuexiao.entity.ResultInfo;
import com.yuexiao.pojo.AddressBook;
import com.yuexiao.pojo.Employee;
import com.yuexiao.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName:AddressBookController
 * @Auther: YUEXIAO
 * @Description: 地址管理控制器
 * @Date: 2022/8/13 14:08
 * @Version: v1.0
 */
@RestController
@Slf4j
@RequestMapping("/addressBook")
public class AddressBookController {

    @Reference
    private AddressBookService addressBookService;


    /**
     * 根据用户id查询该用户的地址列表
     *
     * @param request
     * @return
     */
    @GetMapping("/addressList")
    @PreAuthorize("hasAnyRole('ROLE_NOMAL')")
    public ResultInfo addressList(HttpServletRequest request) {
        Object userid = request.getSession().getAttribute("userid");
        if (userid == null) {
            return ResultInfo.error("用户未登录");
        }
        return ResultInfo.success(addressBookService.getAddressBookByUserId((Long) userid));
    }

    /**
     * 新增地址
     *
     * @param addressBook AddressBook 地址实体类
     * @param request     无需手动传入
     * @return ResultInfo 返回结果对象
     */
    @PostMapping("/addAddress")
    @PreAuthorize("hasAnyRole('ROLE_NOMAL')")
    public ResultInfo addAddress(@RequestBody AddressBook addressBook, HttpServletRequest request) {
        addressBook.setUserId((Long) request.getSession().getAttribute("userid"));
        log.info("address:{}", addressBook);
        boolean result = addressBookService.insertAddressBook(addressBook);
        if (result) {
            return ResultInfo.success("新增成功");
        }
        return ResultInfo.error("新增失败");
    }


    /**
     * 根据地址主键设置默认地址
     *
     * @param addressBook AddressBook 图书实体类，只需要传入主键id即可
     * @return ResultInfo 返回结果独享
     */
    @PutMapping("/setDefaultAddress")
    @PreAuthorize("hasAnyRole('ROLE_NOMAL')")
    public ResultInfo setDefaultAddress(@RequestBody AddressBook addressBook) {
        return ResultInfo.success(addressBookService.updateDefaultById(addressBook.getId()));
    }


    /**
     * 根据主键id查询单个地址
     *
     * @param id Long 地址主键id
     * @return ResultInfo 返回结果对象
     */
    @GetMapping("/addressFindOne/{id}")
    @PreAuthorize("hasAnyRole('ROLE_NOMAL')")
    public ResultInfo addressFindOne(@PathVariable Long id) {
        return ResultInfo.success(addressBookService.getAddressBookById(id));
    }


    /**
     * 修改地址数据
     *
     * @param addressBook AddressBook 地址实体类
     * @return ResultInfo 返回结果对象
     */
    @PutMapping("/updateAddress")
    @PreAuthorize("hasAnyRole('ROLE_NOMAL')")
    public ResultInfo updateAddress(@RequestBody AddressBook addressBook) {
        boolean result = addressBookService.updateAddressBook(addressBook);
        if (result) {
            return ResultInfo.success("修改成功");
        }
        return ResultInfo.error("修改失败");
    }


    /**
     * 根据id删除地址数据
     *
     * @param ids Long 地址主键
     * @return ResultInfo 返回结果对象
     */
    @DeleteMapping("/deleteAddress")
    @PreAuthorize("hasAnyRole('ROLE_NOMAL')")
    public ResultInfo deleteAddress(Long ids) {
        boolean result = addressBookService.deleteByIds(ids);
        if (result) {
            return ResultInfo.success("删除成功");
        }
        return ResultInfo.error("删除失败");
    }


    /**
     * 获取当前登录用户的默认地址
     *
     * @param request 无需手动传入
     * @return ResultInfo 返回结果对象
     */
    @GetMapping("/getDefaultAddress")
    @PreAuthorize("hasAnyRole('ROLE_NOMAL')")
    public ResultInfo getDefaultAddress(HttpServletRequest request) {
        Object userid = request.getSession().getAttribute("userid");
        if (userid == null) {
            return ResultInfo.error("用户未登录");
        }
        return ResultInfo.success(addressBookService.getDefaultAddressBookByUserId(Long.valueOf(String.valueOf(userid))));
    }

}
